package com.sltx.controller;

import com.sltx.controller.base.BaseController;
import com.sltx.response.CommonResult;
import com.sltx.service.AuditService;
import com.sltx.util.ExcelUtil;
import com.sltx.util.ExcelUtil2;
import com.sltx.util.ImportExcel;
import com.sltx.util.PageData;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;


@RestController
@ResponseBody
@RequestMapping("/audit")
public class AuditController extends BaseController {

    @Autowired
    private AuditService auditService;


    @RequestMapping(value = "/getUploadedALL")
    public CommonResult<Map<String, Object>> getUploadedALL() {
        PageData pd = this.getPageData();
        Map<String, Object> result = auditService.getUploadedALL(pd);
        return CommonResult.success(result);
    }

    @RequestMapping(value = "/Download")
    public List<Map<String, Object>> Download() {
        PageData pd = this.getPageData();
        List<Map<String, Object>> result = auditService.Download(pd);
        return result;
    }

    @RequestMapping(value = "/DownloadQueryc")
    public Object DownloadQueryc() {
        PageData pd = this.getPageData();
        List<Map<String, Object>> result = auditService.DownloadQueryc(pd);
        Date date = new Date();
        SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
        pd.put("matchDate", dateFormat1.format(date));
        //修改匹配时间和状态（改为匹配中“2”）
        auditService.upDateTime(pd);
        return result;
    }

    @RequestMapping("/import")
    public String getEntNameNum(@RequestParam MultipartFile[] file) {
        PageData pd = this.getPageData();
        System.out.println(pd.get("taskNum"));

        try {
            String fileName1 = file[0].getOriginalFilename();
            String fileName2 = file[1].getOriginalFilename();
            System.out.println(fileName1);
            System.out.println(fileName2);

            InputStream inputStream1 = file[0].getInputStream();
            InputStream inputStream2 = file[1].getInputStream();
            List<Map<String, Object>> list1 = ImportExcel.readExcel(fileName1, inputStream1);
            List<Map<String, Object>> list2 = ImportExcel.readExcel(fileName2, inputStream2);
            auditService.insertExcel1(list1, pd);
            auditService.insertExcel2(list2, pd);
            return "上传成功";
        } catch (Exception e) {
            return e.toString();
        }
    }

    //桥接接口
    @RequestMapping(
            value = {"/getUploadedALL2"}
    )
    public CommonResult<Map<String, Object>> getUploadedALL2() {
        PageData pd = this.getPageData();
        Map<String, Object> result = this.auditService.getUploadedALL2(pd);
        return CommonResult.success(result);
    }

    //桥接接口
    @RequestMapping({"/Download2"})
    public String Download2(HttpServletResponse response) {
        PageData pd = this.getPageData();

        List<String> rowNameList = Arrays.asList("任务id", "企业名称", "统一社会信用代码", "关联层级", "用户名");
        List<Map<String, String>> list = new ArrayList();
        List<Map<String, Object>> result = this.auditService.Download2(pd);
        if (CollectionUtils.isEmpty(result)) {
            return "下载失败";
        } else {
            Iterator var6 = result.iterator();

            while(var6.hasNext()) {
                Map m = (Map)var6.next();
                Map<String, String> map = new HashMap();
                String taskNum = m.get("taskNum").toString();
                String entname = m.get("entname").toString();
                String uniscid = m.get("uniscid").toString();
                String connum = m.get("connum").toString();
                String userName = m.get("userName").toString();
                map.put("任务id", taskNum);
                map.put("企业名称", entname);
                map.put("统一社会信用代码", uniscid);
                map.put("关联层级", connum);
                map.put("用户名", userName);
                list.add(map);
            }

            ExcelUtil2.excelPort(pd.get("taskName").toString() + "企业详情", rowNameList, list, response);
            return "下载成功";
        }
    }



    //桥接接口
    @RequestMapping({"/DownloadQueryc2"})
    public String DownloadQueryc(HttpServletResponse response) {
        PageData pd = this.getPageData();
        List<String> rowNameList = Arrays.asList("任务id", "关联单位");
        List<Map<String, String>> list = new ArrayList();
        List<Map<String, Object>> result = this.auditService.DownloadQueryc2(pd);
        if (CollectionUtils.isEmpty(result)) {
            return "下载失败";
        } else {
            Iterator var6 = result.iterator();
            while(var6.hasNext()) {
                Map m = (Map)var6.next();
                Map<String, String> map = new HashMap();
                String taskNum = m.get("task_num").toString();
                String queryC = m.get("query_c").toString();
                map.put("任务id", taskNum);
                map.put("关联单位", queryC);
                list.add(map);
            }
            ExcelUtil.excelPort(pd.get("taskName").toString() + "关联单位", rowNameList, list, response);
            Date date = new Date();
            SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
            pd.put("matchDate", dateFormat1.format(date));
            this.auditService.upDateTime2(pd);
            return "下载成功";
        }
    }

    //桥接接口
    @RequestMapping({"/import2"})
    public String getEntNameNum2(@RequestParam MultipartFile[] file) {
        PageData pd = this.getPageData();
        try {
            String fileName1 = file[0].getOriginalFilename();
            String fileName2 = file[1].getOriginalFilename();
            System.out.println(fileName1);
            System.out.println(fileName2);
            InputStream inputStream1 = file[0].getInputStream();
            InputStream inputStream2 = file[1].getInputStream();
            List<Map<String, Object>> list1 = ImportExcel.readExcel(fileName1, inputStream1);
            List<Map<String, Object>> list2 = ImportExcel.readExcel(fileName2, inputStream2);
            Object o1 = this.auditService.insertExcel12(list1, pd);
            if (o1.equals("上传成功")) {
                Object o2 = this.auditService.insertExcel22(list2, pd);
                return o2.equals("上传成功") ? "上传成功" : "上传失败";
            } else {
                return "上传失败";
            }
        } catch (Exception var11) {
            return var11.toString();
        }
    }

}



